﻿using System;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using ApplLogic.ApplClasses;
using ConnectionObjects;
using TCPClient.RequestsToServerHandlers;
using TCPClient.UI;

namespace TCPClient
{
    public class Client
    {
        private TcpClient m_TCPClient = new TcpClient();
        private IPEndPoint m_ServerEndPoint = null;
        private NetworkStream m_Stream = null;
        private UIAbstract m_UI;

        public Client()
        {
            createConnetionToServer();
            if (m_TCPClient.Connected)
            {
                Console.WriteLine("Successuly connected with server");
                handleLogin();
                m_UI.comunicateWithServer();
            }

        }

        private void handleLogin()
        {
            LoginUI loginUi = new LoginUI();
            object responce = loginUi.LoginForm(m_Stream);
            m_UI = responce is ApplWorker
                       ? (UIAbstract)
                         new WorkerUI(new RequestsToServerHandlerForWorker(m_Stream, responce as ApplWorker))
                       : new ManagerUI(new RequestsToServerHandlerForManager(m_Stream, responce as ApplManager));
        }

        /// <summary>
        /// 
        /// </summary>
        private void createConnetionToServer()
        {
            m_ServerEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.103"), 3000);
            m_TCPClient.Connect(m_ServerEndPoint);
            m_Stream = m_TCPClient.GetStream();
        }  
    }

}
